//
//  ObjExtension.swift
//  ChiShui
//
//  Created by 邹琳 on 2018/1/9.
//  Copyright © 2018年 邹琳. All rights reserved.
//

import UIKit

//MARK:-----------随机颜色-------------//
extension UIColor{
    class func randomColor()->UIColor{
        return UIColor(red: CGFloat(arc4random_uniform(100)) / 255.0, green: CGFloat(arc4random_uniform(100)) / 255.0, blue: CGFloat(arc4random_uniform(100)) / 255.0, alpha: 1.0)
    }
    
    convenience init(red: Int, green: Int, blue: Int) {
        assert(red >= 0 && red <= 255, "Invalid red component")
        assert(green >= 0 && green <= 255, "Invalid green component")
        assert(blue >= 0 && blue <= 255, "Invalid blue component")
        
        self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
    }
    
    convenience init(_ rgb: Int) {
        self.init(
            red: (rgb >> 16) & 0xFF,
            green: (rgb >> 8) & 0xFF,
            blue: rgb & 0xFF
        )
    }
}

extension UIFont {
    class func mediumFont(_ fontSize: Int) -> UIFont {
        guard let f =  UIFont(name: "PingFangSC-Medium", size: CGFloat(fontSize)) else{
            return UIFont.boldSystemFont(ofSize: CGFloat(fontSize))
        }
        return f
    }
}

extension String{
    /** 字符串转时间 */
    func getDate() -> Date{
        let matter = DateFormatter()
        matter.dateFormat = "yyyy-MM-dd HH:mm"
        return matter.date(from: self) ?? Date()
    }
    
    /** 字符串儿转时间戳 */
    func getDateInterval() -> String{
        let matter = DateFormatter()
        matter.dateFormat = "yyyy-MM-dd HH:mm"
        if let date = matter.date(from: self){
            return "\(Int64(date.timeIntervalSince1970))"
        }else{
            return "字符串转时间戳失败"
        }
    }
    
    /** 时间戳转日期 */
    func IntervalToDateString() -> String{
        let timeint = Int(self)
        let time = TimeInterval(timeint ?? 1463241600)
        let date = Date(timeIntervalSince1970: time)
        let matter = DateFormatter()
        matter.dateFormat = "yyyy-MM-dd"
        return matter.string(from: date)
    }
    /** 时间戳转日期 */
    func IntervalToDateString2() -> String{
        let timeint = Int(self)
        let time = TimeInterval(timeint ?? 1463241600)
        let date = Date(timeIntervalSince1970: time)
        let matter = DateFormatter()
        matter.dateFormat = "yyyy-MM-dd HH:mm"
        return matter.string(from: date)
    }
    
    /** 计算文字高度 */
    func get_heightForComment(fontSize: CGFloat, width: CGFloat) -> CGFloat {
        let font = UIFont.systemFont(ofSize: fontSize)
        let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return ceil(rect.height)
    }
    
    
    
    
    /** 获取缓存数据 */
    static func getCachesSize() -> String{
        
        let file = FileManager.default
        let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).last ?? ""
        let isExist = file.fileExists(atPath: path)//计算缓存目录的大小
        var size = 0
        if isExist {
            //取出文件下所有的文件数组
            guard let child = file.subpaths(atPath: path) else { return "0" }
            //枚举出所有文件名，计算文件大小
            for tempFile in child{
                //拼接文件名到路径
                let temppath = "\(path)/\(tempFile)"
                //去除文件属性
                let folder = try! file.attributesOfItem(atPath: temppath)
                for (key,value) in folder{
                    if key == FileAttributeKey.size {
                        size = size + (value as? Int)!
                    }
                }
            }
        }
        let finalSize = String(format:"%.2f",Float(size) / 1024.0 / 1024.0)
        return "\(finalSize)M"
    }
    
    /** 获取App版本号 */
    static func getAppVersion() -> String{
        guard let dic = Bundle.main.infoDictionary else { return "1.0" }
        let value = dic["CFBundleShortVersionString"]
        return value as! String
    }
    
    /** 清楚缓存 */
    static func clearCaches(){
        let file = FileManager.default
        let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).last ?? ""
        //取出文件下所有的文件数组
        guard let child = file.subpaths(atPath: path) else { return }
        //枚举出所有文件名，计算文件大小
        for tempFile in child{
            //拼接文件名到路径
            let temppath = "\(path)/\(tempFile)"
            //删除文件
            if file.fileExists(atPath: temppath) {
                try? file.removeItem(atPath: temppath)
                print("清除成功")
            }
        }
    }
    
    func isPhoneNum() -> Bool{
        //是否是电话号码
        let check = "^1[3-8][0-9]\\d{8}$"
        let test = NSPredicate(format: "SELF MATCHES %@", check)
        return test.evaluate(with:self)
    }
}

extension Date{
    
    /** 时间转字符串 */
    func getStringDate() -> String{
        let matter = DateFormatter()
        matter.dateFormat = "yyyy-MM-dd HH:mm"
        return matter.string(from: self)
    }
}


//MARK: - UIDevice延展
public extension UIDevice {
    
    var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        
        switch identifier {
        case "iPod5,1":                                 return "iPod Touch 5"
        case "iPod7,1":                                 return "iPod Touch 6"
        case "iPhone3,1", "iPhone3,2", "iPhone3,3":     return "iPhone 4"
        case "iPhone4,1":                               return "iPhone 4s"
        case "iPhone5,1", "iPhone5,2":                  return "iPhone 5"
        case "iPhone5,3", "iPhone5,4":                  return "iPhone 5c"
        case "iPhone6,1", "iPhone6,2":                  return "iPhone 5s"
        case "iPhone7,2":                               return "iPhone 6"
        case "iPhone7,1":                               return "iPhone 6 Plus"
        case "iPhone8,1":                               return "iPhone 6s"
        case "iPhone8,2":                               return "iPhone 6s Plus"
        case "iPhone9,1":                               return "iPhone 7 (CDMA)"
        case "iPhone9,3":                               return "iPhone 7 (GSM)"
        case "iPhone9,2":                               return "iPhone 7 Plus (CDMA)"
        case "iPhone9,4":                               return "iPhone 7 Plus (GSM)"
            
        case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
        case "iPad3,1", "iPad3,2", "iPad3,3":           return "iPad 3"
        case "iPad3,4", "iPad3,5", "iPad3,6":           return "iPad 4"
        case "iPad4,1", "iPad4,2", "iPad4,3":           return "iPad Air"
        case "iPad5,3", "iPad5,4":                      return "iPad Air 2"
        case "iPad2,5", "iPad2,6", "iPad2,7":           return "iPad Mini"
        case "iPad4,4", "iPad4,5", "iPad4,6":           return "iPad Mini 2"
        case "iPad4,7", "iPad4,8", "iPad4,9":           return "iPad Mini 3"
        case "iPad5,1", "iPad5,2":                      return "iPad Mini 4"
        case "iPad6,7", "iPad6,8":                      return "iPad Pro"
        case "AppleTV5,3":                              return "Apple TV"
        case "i386", "x86_64":                          return "Simulator"
        case "iPhone10,3","iPhone10,6": 				return "iPhoneX"
        case "iPhone11,2":                              return "iPhoneXS"
        case "iPhone11,4","iPhone11,6":                 return "iPhoneXSMax"
        case "iPhone11,8":                 				return "iPhoneXR"
        default:                                        return identifier
        }
    }
    
}

/** 国际化的拓展 */
extension String{
    var zlLocalizedString:String{
        get{
            return NSLocalizedString(self, comment: self)
        }
    }
}

